package swind.web.dao.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import swind.common.md5.MD5Utils;
import swind.web.dao.IRoleDao;
import swind.web.dao.IUserDao;
import swind.web.dao.base.BaseHibernateDAO;
import swind.web.entities.ExamRole;
import swind.web.entities.ExamUser;
import swind.web.exception.UserExistenceException;
import swind.web.vo.TeaGetStudentVo;

@Repository(value="userDaoImp")
public class UserDaoImp extends BaseHibernateDAO<ExamUser> implements IUserDao{
	
	@Resource(name="roleDaoImp")
	IRoleDao roleDaoImp;

	@Override
	public void save(List<TeaGetStudentVo> stuList) throws ParseException, UserExistenceException {
		// TODO Auto-generated method stub
		for(TeaGetStudentVo stu:stuList){
			if(this.get(stu.getUserId())!=null){
				throw new UserExistenceException();
			}
			
			this.save(stu);
		}
		
	}

	@Override
	public void save(TeaGetStudentVo getStudentVo) throws ParseException {
		ExamRole studentRole = roleDaoImp.get(25);
		ExamUser user = new ExamUser();
		user.setPassword(MD5Utils.GetMD5Code(user.getUserName()));
		BeanUtils.copyProperties(getStudentVo, user);
		
		SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy");
		user.setBirthday(sf.parse(getStudentVo.getStrbirthday()));
		
		this.save(user);
		user.getExamRoles().add(studentRole);
		
	}

	@Override
	public void deleteStudentWithRole(String stuId) {
		ExamUser user = this.get(stuId);
		ExamRole role = roleDaoImp.get(25);
		user.getExamRoles().remove(role);
		this.deleteByKey(stuId);
		
	}
	
	
}
